package samples.d3.image 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.utils.setInterval;
	import samples.BaseSample;
	import utils.GeomUtils;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class PaletteMap extends BaseSample 
	{
		[Embed(source = '../../../../lib/lemur.jpg')]private var src:Class;
		private var bd:BitmapData;
		private var bmp:Bitmap;
		private var redArray:Array;
		private var greenArray:Array;
		private var blueArray:Array;
		public function PaletteMap() 
		{
			bmp = new src();
			addChild( bmp );
			
			bd = new BitmapData( bmp.width, bmp.height, true, 0 );
			addChild( new Bitmap( bd ) );
			
			redArray = new Array(256);
			greenArray = new Array(256);
			blueArray = new Array(256);
			center();
			interval = setInterval( update, 30 );
			
		}
		
		private function update():void 
		{
			
			var val:int;
			var colNum:int = ( _stage.mouseY / sh ) * 0x10;
			for (var i:uint = 0; i < 0xFF; i++)
			{
				val = int( ( GeomUtils.map( (i+1), 0, 0xFF, 0, colNum ) ) );
				val *= (0xFF / colNum );
				
				redArray[i] 	= 0xFF000000 | val << 16;
				greenArray[i] 	= 0xFF000000 | val << 8;
				blueArray[i] 	= 0xFF000000 | val;
				
			}
			
			bd.fillRect( bd.rect, 0 );
			bd.draw( bmp );
			
			bd.paletteMap(bd, bd.rect, new Point(), redArray, greenArray, blueArray);
		
		}
		
	}

}